package com.qqtech.qquan.community.service;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.dao.BaseDao;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.service.BaseServiceImpl;
import com.qqtech.qquan.circle.service.CircleTopicService;
import com.qqtech.qquan.community.dao.CommunityMessageDao;
import com.qqtech.qquan.community.enums.CommunityMessageStatusDictEnum;
import com.qqtech.qquan.community.model.CommunityMessage;
import com.qqtech.qquan.community.model.CommunityMessageQuery;
import com.qqtech.qquan.community.model.CommunityMessageVo;
import com.qqtech.qquan.hi.service.HiCommunityService;

@Service
public class CommunityMessageServiceImpl extends BaseServiceImpl<CommunityMessage> implements CommunityMessageService {
	public static final Logger log = LoggerFactory.getLogger(CommunityMessageServiceImpl.class);

	@Resource
	private CommunityMessageDao communityMessageDao;
	
	@Resource
	private CircleTopicService circleTopicService;
	
	@Resource
	private HiCommunityService hiCommunityService;
	
	@Override
	protected BaseDao<CommunityMessage> getBaseDao() {
		return communityMessageDao;
	}

	@Override
	public int increasePv(Integer id) {
		return communityMessageDao.updatePv(id);
	}
	
	@Override
	public Page<CommunityMessageVo> queryPageListAdmin(CommunityMessageQuery query, Pageable pageable) {
		return communityMessageDao.queryPageListAdmin(query, pageable);
	}

	@Override
	public CommunityMessageVo handleDetail(Integer id) {
		communityMessageDao.updatePv(id);
		CommunityMessageVo communityMessageVo = communityMessageDao.getById(id);
		return communityMessageVo;
	}

	@Override
	public int hide(Integer id) {
		CommunityMessage communityMessage = new CommunityMessage();
		communityMessage.setId(id);
		communityMessage.setStatus(CommunityMessageStatusDictEnum.DELETE.getCode());
		communityMessage.setIsShow(YesNoEnum.NO.getCode());
		return communityMessageDao.updateById(communityMessage);
	}

	@Override
	public int show(Integer id) {
		CommunityMessage communityMessage = new CommunityMessage();
		communityMessage.setId(id);
		communityMessage.setStatus(CommunityMessageStatusDictEnum.NORMAL.getCode());
		communityMessage.setIsShow(YesNoEnum.YES.getCode());
		return communityMessageDao.updateById(communityMessage);
	}

	@Override
	public void handleAdd(CommunityMessageVo communityMessageVo) {
		communityMessageDao.insert(communityMessageVo);
		if(null!=communityMessageVo.getCommunityId()){
			//荣誉系统-ryz
			hiCommunityService.addMessage(communityMessageVo.getCreatorId(), communityMessageVo.getCommunityId());
		}
	}

	@Override
	public void increaseComment(Integer id, int addCount) {
		communityMessageDao.increaseComment(id, addCount);
		
	}

	@Override
	public void increasePraise(Integer id, int addCount) {
		communityMessageDao.increasePraise(id, addCount);
	}

	@Override
	public int queryCountFriendUp(CommunityMessageQuery query) {
		return communityMessageDao.queryCountFriendUp(query);
	}

	@Override
	public Page<CommunityMessageVo> queryPageFriendUp(CommunityMessageQuery query, Pageable pageable) {
		return communityMessageDao.queryPageFriendUp(query, pageable);
	}
	
	@Override
	public List<CommunityMessageVo> myLastSomeMessages(Integer userId, Integer some) {
		CommunityMessageQuery messageQuery=new CommunityMessageQuery();
		messageQuery.setCreatorId(userId);
		messageQuery.setStatus(CommunityMessageStatusDictEnum.NORMAL.getCode());
		Pageable pageKit = new PageKit(1,some, new PageOrder(Direction.DESC, "newAt"));
		List<CommunityMessageVo> messageList = communityMessageDao.queryList(messageQuery, pageKit);
		return messageList;
	}

	@Override
	public CommunityMessageVo queryTotalCountByCreatorId(Integer creatorId) {
		CommunityMessageQuery query = new CommunityMessageQuery();
		query.setCreatorId(creatorId);
		return communityMessageDao.queryTotalCountByCreatorId(query);
	}

	@Override
	public List<CommunityMessageVo> queryTotalCountByCreatorIds(List<Integer> creatorIds) {
		CommunityMessageQuery query = new CommunityMessageQuery();
		query.setUserIds(creatorIds);
		return communityMessageDao.queryTotalCountByCreatorIds(query);
	}
}